package com.hejing.TCP与UDP网络编程;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * ClassName: TCPTest02
 * Package: com.hejing.TCP与UDP网络编程
 * Description:
 *      客户端发送文件给服务器，服务端将文件保存在本地
 * @Author: HeJing
 * @Create: 2023-06-02 20:08
 * @Version: 1.0
 */
public class TCPTest02 {
    @Test
    public void client() {
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            // 1、创建Socket
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");  // 指明服务器端的ip地址
            int port = 9090;  // 指明服务器端的端口号

            socket = new Socket(inetAddress, port);

            // 2、创建File的实例、FileInputStream的实例
            File file = new File("D:\\图片\\Camera Roll\\十元.jpg");
            fis = new FileInputStream(file);

            // 3、通过socket获取输出流
            os = socket.getOutputStream();

            // 4、读取数据
            byte[] buffer = new byte[1024];
            int size;
            while((size = fis.read(buffer)) != -1) {
                os.write(buffer, 0, size);
            }
            System.out.println("数据发送完毕...");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 5、关闭socket和相关的流
            try {
                if(os != null)
                    os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;  // 拿到了客户端的socket
        InputStream is = null;
        FileOutputStream fos = null;

        try {
            // 1、创建ServerSocket
            int port = 9090;
            serverSocket = new ServerSocket(port);

            // 2、接收来自于客户端的socket: accept()
            socket = serverSocket.accept();

            // 3、通过socket获取一个输入流
            is = socket.getInputStream();

            // 4、创建File类的实例、FileOutputStream的实例
            File file = new File("D:\\HeJing_Java\\io_test\\image_test\\十元.jpg");
            fos = new FileOutputStream(file);

            // 5、具体的读写过程
            byte[] buffer = new byte[1024];
            int size;
            while((size = is.read(buffer)) != -1) {
                fos.write(buffer, 0, size);
            }
            System.out.println("数据接收完毕...");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 6、关闭相关的socket和流
            try {
                if(fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(serverSocket != null)
                    serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


















